<template>
  <div class="settings-view">
    <h1 class="page-title">
      <Icon icon="lucide:settings" width="28" height="28" />
      设置中心
    </h1>

    <el-row :gutter="24">
      <el-col :xs="24" :md="12">
        <el-card class="card-container">
          <template #header>
            <div class="card-header">
              <span>个人信息</span>
            </div>
          </template>

          <el-form :model="profileForm" label-width="100px">
            <el-form-item label="用户名">
              <el-input v-model="authStore.user.username" disabled />
            </el-form-item>

            <el-form-item label="真实姓名">
              <el-input v-model="profileForm.realName" />
            </el-form-item>

            <el-form-item label="邮箱">
              <el-input v-model="profileForm.email" />
            </el-form-item>

            <el-form-item label="手机号">
              <el-input v-model="profileForm.phone" />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleUpdateProfile">更新信息</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>

      <el-col :xs="24" :md="12">
        <el-card class="card-container">
          <template #header>
            <div class="card-header">
              <span>修改密码</span>
            </div>
          </template>

          <el-form :model="passwordForm" :rules="passwordRules" ref="passwordFormRef" label-width="100px">
            <el-form-item label="原密码" prop="oldPassword">
              <el-input v-model="passwordForm.oldPassword" type="password" show-password />
            </el-form-item>

            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model="passwordForm.newPassword" type="password" show-password />
            </el-form-item>

            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input v-model="passwordForm.confirmPassword" type="password" show-password />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleChangePassword">修改密码</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="24" style="margin-top: 24px;">
      <el-col :span="24">
        <el-card class="card-container">
          <template #header>
            <div class="card-header">
              <span>系统信息</span>
            </div>
          </template>

          <el-descriptions :column="2" border>
            <el-descriptions-item label="系统名称">
              图书管理系统
            </el-descriptions-item>
            <el-descriptions-item label="版本号">
              v1.0.0
            </el-descriptions-item>
            <el-descriptions-item label="前端框架">
              Vue 3 + Vite
            </el-descriptions-item>
            <el-descriptions-item label="UI 框架">
              Element Plus
            </el-descriptions-item>
            <el-descriptions-item label="后端框架">
              Spring Boot 3.x
            </el-descriptions-item>
            <el-descriptions-item label="数据库">
              MySQL 8.0
            </el-descriptions-item>
          </el-descriptions>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="24" style="margin-top: 24px;">
      <el-col :span="24">
        <el-card class="card-container">
          <template #header>
            <div class="card-header">
              <span>操作日志</span>
            </div>
          </template>

          <el-timeline>
            <el-timeline-item
              v-for="(activity, index) in activities"
              :key="index"
              :timestamp="activity.timestamp"
            >
              {{ activity.content }}
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Icon } from '@iconify/vue'
import { useAuthStore } from '@/store/auth'

const authStore = useAuthStore()

const profileForm = reactive({
  realName: '',
  email: '',
  phone: ''
})

const passwordForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const passwordFormRef = ref()

const passwordRules = {
  oldPassword: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const activities = ref([
  {
    content: '用户登录系统',
    timestamp: '2024-01-07 10:30:00'
  },
  {
    content: '修改了个人信息',
    timestamp: '2024-01-06 15:20:00'
  },
  {
    content: '借阅了《Java核心技术》',
    timestamp: '2024-01-05 14:10:00'
  },
  {
    content: '注册了系统账户',
    timestamp: '2024-01-02 09:00:00'
  }
])

const handleUpdateProfile = () => {
  // 模拟更新
  ElMessage.success('个人信息更新成功')
}

const handleChangePassword = async () => {
  if (!passwordFormRef.value) return

  await passwordFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        // 模拟修改密码
        await new Promise(resolve => setTimeout(resolve, 1000))
        ElMessage.success('密码修改成功')
        passwordForm.oldPassword = ''
        passwordForm.newPassword = ''
        passwordForm.confirmPassword = ''
      } catch (error) {
        ElMessage.error('密码修改失败')
      }
    }
  })
}

onMounted(() => {
  // 初始化表单数据
  profileForm.realName = authStore.user?.realName || ''
  profileForm.email = authStore.user?.email || ''
  profileForm.phone = authStore.user?.phone || ''
})
</script>

<style scoped>
.settings-view {
  padding: 0;
}

.card-header {
  font-weight: 600;
  color: #303133;
}
</style>
